<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<jsp:useBean id="warrantystarttime" class="java.util.Date"/>
<c:set target="${warrantystarttime}" property="time" value="${umbrellaHardwareProductSerialnumber.warrantystarttime}"/>
<jsp:useBean id="warrantyendtime" class="java.util.Date"/>
<c:set target="${warrantyendtime}" property="time" value="${umbrellaHardwareProductSerialnumber.warrantyendtime}"/>

<div id="deliverDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="direction">
                    <span class="help-inline"><font color="red">*</font> </span>
                    使用去向：</label>
                <div class="col-sm-5">
                    <select id="direction" name="direction" class="form-control">
                        <c:forEach var="upmsDict" items="${directionList}">
                            <option value="${upmsDict.dicValue}" <c:if test="${umbrellaHardwareProductSerialnumber.direction==upmsDict.dicValue}">selected="selected"</c:if>>${upmsDict.dicText}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="agent" >代理：</label>
                <div class="col-sm-5">
                    <div id="agent" data-id="${agent.id}" data-name="${agent.name}"></div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="directionremark">使用去向备注：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="directionremark" name="directionremark" placeholder="使用去向备注" maxlength="100" value="${umbrellaHardwareProductSerialnumber.directionremark}"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="pathcount">
                    <span class="help-inline"><font color="red">*</font> </span>
                    分机数量：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="pathcount" name="pathcount" placeholder="分机数量(伞道等)" maxlength="2" value="${umbrellaHardwareProductSerialnumber.pathcount==null?1:umbrellaHardwareProductSerialnumber.pathcount}"  onkeyup="value=value.replace(/[^\d]/g,'')"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="warrantystarttime">保修开始时间：</label>
                <div class="col-sm-5">
                    <input type="text" id="warrantystarttime"  class="form-control" data-date-format="yyyy-mm-dd" placeholder="保修开始时间" value="<fmt:formatDate value="${warrantystarttime}" pattern="yyyy-MM-dd" />">
                    <input value="" type="text" hidden name="warrantystarttime" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="warrantyendtime">保修结束时间：</label>
                <div class="col-sm-5">
                    <input type="text" id="warrantyendtime"  class="form-control" data-date-format="yyyy-mm-dd" placeholder="保修结束时间" value="<fmt:formatDate value="${warrantyendtime}" pattern="yyyy-MM-dd" />">
                    <input value="" type="text" hidden name="warrantyendtime" >
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="deliverDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
                direction: {
                    required: true
                },
                pathcount: {
                    required: true,
                    number:true,
                    max:99,
                    min:1
                },
                directionremark: {
                    maxlength: 100
                }
            },
            messages: {
                direction: {
                    required: "使用去向是必选项。"
                },
                pathcount: {
                    required: "分机数量是必填项。",
                    number:"请输入有效数字",
                    max:"最大限制为99",
                    min:"最小限制为1"
                },
                directionremark: {
                    maxlength: "使用去向备注长度超出100个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }

    $(function(){
        $('#warrantystarttime,#warrantyendtime').datepicker();
        //初始化树形select控件
        $("#agent").ztreeSelect({
            name:"agentid",
            url: '${basePath}/manage/agentTree',
            params: {isLeaf: 1},
            renderId: "agent",
            callback: function (nodeIds, treeNode) {
            }
        })
    });

    //注册表单验证
    $(validateForm());

    function createSubmit() {
        if(validateForm().form()){
            if($("#warrantystarttime").val())
                $("input[name='warrantystarttime']").val(getLongFromatDateStr($("#warrantystarttime").val()));
            if($("#warrantyendtime").val())
                $("input[name='warrantyendtime']").val(getLongFromatDateStr($("#warrantyendtime").val()));
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/serialnum/deliver/${ids}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        infoDialog("成功更新【" + result.data + "】台。");
                        deliverDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>